package com.kotlin.demo

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request

class Test02Suspend {
    fun main() {
        runBlocking {
            requestDoc();
        }

    }

    private suspend fun requestDoc() {
        val result = get("https://www.wanandroid.com/banner/json")
        println(result)
    }

    suspend fun get(url: String): String? = withContext(Dispatchers.IO) {
        return@withContext OkHttpClient().newCall(Request.Builder().url(url).build())
            .execute().body?.string();
    }
}